angular
		.module('app', [ 'ngCookies' ])

//		.factory('cordovaReady', function() {
//			return function(fn) {
//
//				var queue = []
//
//				var impl = function() {
//					queue.push(Array.prototype.slice.call(arguments))
//				}
//
//				document.addEventListener('deviceready', function() {
//					queue.forEach(function(args) {
//						fn.apply(this, args)
//					})
//					impl = fn
//				}, false)
//
//				return function() {
//					return impl.apply(this, arguments)
//				}
//			}
//			
//			
//		})

		.factory('Server', function() {

			return {
				session : '',
				// host : 'http://localhost:5000'
				host : 'http://qenix.herokuapp.com'
			}
		})

		.controller(
				'appCtrl',
				[
						'$scope',
						'$http',
						'$location',
						'Server',
						function($scope, $http, $location, Server) {
							$scope.appName = "FoodTruck";
							$scope.galleryDataList;
							$scope.slideDataList;
							$scope.setDataList = function() {
								var responsePromise = $http
										.jsonp(Server.host
												+ "/api/get/getGalleryItem.json?callback=JSON_CALLBACK&"
												+ "session=" + Server.session);

								responsePromise.success(function(data) {
									$scope.galleryDataList = data.rows
									console.log($scope.galleryDataList)

								});

								responsePromise = $http
										.jsonp(Server.host
												+ "/api/get/slideData.json?callback=JSON_CALLBACK&"
												+ "session=" + Server.session);

								responsePromise.success(function(data) {
									$scope.slideDataList = data.rows

								});

								window.location.href = "#main-content";
							}

							$scope.$on('login success', function(event) {
								$scope.setDataList()
							});

							$scope.$on('logout', function(event) {
								window.location.href = "#login"
							});

						} ])

		.controller(
				'LoginCtrl',
				[
						'$scope',
						'$http',
						'$cookies',
						'Server',
						function($scope, $http, $cookies, Server) {

							$scope.user = {
								email : undefined,
								password : undefined,
								check : false
							};

							if ($cookies.check) {
								$scope.user.email = $cookies.email;
								$scope.user.password = $cookies.password;
								$scope.user.check = $cookies.check;
							}

							$scope.loginSubmit = function() {

								var dataObject = {
									email : $scope.user.email,
									password : $scope.user.password
								};

								var responsePromise = $http
										.jsonp(Server.host
												+ "/api/json/login?callback=JSON_CALLBACK&"
												+ "email=" + $scope.user.email
												+ "&password="
												+ $scope.user.password);

								responsePromise
										.success(function(data, status,
												headers, config) {

											switch (data.code) {

											case 200:

												Server.session = $scope.user.email;
												if ($scope.user.check) {
													$cookies.email = $scope.user.email;
													$cookies.password = $scope.user.password;
													$cookies.check = true;
												} else {
													$cookies.email = undefined;
													$cookies.password = undefined;
													$scope.user.email = undefined;
													$scope.user.password = undefined;
													$cookies.check = false;
												}

												$scope.$emit('login success');
												break

											case 509:
												alert(data.msg);
												break;

											case 510:
												alert(data.msg);
												break;

											}

										})

								responsePromise.error(function(data, status,
										headers, config) {
									alert('통신 실패')
								});

							}

						} ])
		.controller(
				'LogOutCtrl',
				[
						'$scope',
						'$http',
						'Server',
						function($scope, $http, Server) {

							$scope.logOut = function() {

								var responsePromise = $http
										.jsonp(Server.host
												+ "/api/json/logOut?callback=JSON_CALLBACK&"
												+ "&session=" + Server.session);

								responsePromise.success(function(data, status,
										headers, config) {

									Server.session = undefined;
									$scope.$emit('logout');

								});

								responsePromise.error(function(data, status,
										headers, config) {
									alert('통신 실패');
								});

							}

						} ])

		.directive('ngReallyClick', [ function() {
			return {
				restrict : 'A',
				link : function(scope, element, attrs) {
					element.bind('click', function() {
						var message = attrs.ngReallyMessage;
						if (message && confirm(message)) {
							scope.$apply(attrs.ngReallyClick);
						}
					});
				}
			}
		} ]);